package com.yyys.web.controller;

import com.yyys.web.entity.DRInfo;
import com.yyys.web.json.DRGoodsJson;
import com.yyys.web.service.DRGoodsInfoService;
import com.yyys.web.service.DRInfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengsenwen on 2017/7/8.
 */

@Controller
@RequestMapping("/view")
public class ViewController {

    private static Logger logger = Logger.getLogger(ViewController.class);

    @Autowired
    private DRInfoService drInfoService;

    @Autowired
    private DRGoodsInfoService drGoodsInfoService;

    /**
     * 首页餐厅信息 真
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String main(Model model) {
        model.addAttribute("flag", "餐厅实时信息");
        return "main";
    }


    /**
     * 各个餐厅的详情页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/diningroom/{id}", method = RequestMethod.GET)
    public String diningroom(Model model, @PathVariable("id") Integer id) {

        model.addAttribute("flag", "餐厅信息");
        model.addAttribute("diningroomId", String.valueOf(id));

        try {
            //在introduce.html处调用
            DRInfo drInfo = drInfoService.getDrInfoById(id);
            model.addAttribute("drInfo", drInfo);
        } catch (Exception e) {
            logger.info("diningroom:" + e.getMessage());
            e.printStackTrace();
        }

        return "diningroom";
    }

    /**
     * 每日推荐点进去的菜品详情页，是每一道菜的详情页
     */
    @RequestMapping(value = "/diningroom/{drId}/goods/{spId}", method = RequestMethod.GET)
    public String getGoodsByGoodsId(Model model, @PathVariable("drId") Integer drId, @PathVariable("spId") String spId) {

        List<DRGoodsJson> drGoodsJsonList = new ArrayList<>();
        try {
            drGoodsJsonList = drGoodsInfoService.getRecommendGoodBydrIdAndspId(drId, spId);
            model.addAttribute("drGoodsJsonList", drGoodsJsonList);
        } catch (Exception e) {
            logger.info("getGoodsByGoodsId:" + e.getMessage());
            e.printStackTrace();
        }
        return "goodsdetails";
    }


    /**
     * 联系我们
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/contact", method = RequestMethod.GET)
    public String contactUs(Model model) {

        List<DRInfo> drInfoList = new ArrayList<>();
        try {
            drInfoList = drInfoService.getAll();
            model.addAttribute("drInfoList", drInfoList);

        } catch (Exception e) {
            logger.info("contactUs:" + e.getMessage());
            e.printStackTrace();
        }

        model.addAttribute("flag", "联系我们");
        return "contact";
    }
}
